var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
	conn.createChannel(function(err, ch) {

		ch.assertQueue('', { // replyTo queue
			exclusive: true
		}, function(err, q) {
			var corrId = generateUuid();
			var msg = 'hello';
			var start;
			// listen replyTo queue
			ch.consume(q.queue, function(msg) {
				var timeCost = new Date().getTime() - start;
				// on my machine timecost = 4~13 ms
				console.log(' [.] getReply %s, corrId=%s, time=%s', msg.content, msg.properties.correlationId, timeCost);
				setTimeout(function() {
					conn.close();
					process.exit(0);
				}, 500);
			}, {
				noAck: true
			});

			// send request
			start = new Date().getTime();
			console.log(' [x] Requesting %s, corrId=%s, replyTo=%s', msg, corrId, q.queue);
			ch.sendToQueue('rpc_queue', new Buffer('hello'), {
				correlationId: corrId,
				replyTo: q.queue
			});
		});
	});
});

function generateUuid() {
	return Math.random().toString() +
		Math.random().toString();
}